package com.efast.dmom.mes.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.efast.cafe.util.StringUtil;
import com.efast.dmom.mes.bean.Plant;
import com.efast.dmom.mes.bean.ResultBean;
import com.efast.dmom.mes.bean.Workshop;
import com.efast.dmom.mes.datahandlers.entity.PlantDataHandler;
import com.efast.dmom.mes.datahandlers.entity.WorkShopDataHandler;
import com.efast.dmom.mes.exception.MESException;
import com.efast.dmom.mes.service.PlantService;
import com.efast.dmom.mes.util.StringVerifyUtil;
@Service
public class PlantServiceImpl implements PlantService {
	@Autowired
	private PlantDataHandler plantDataHandler;
	
	@Autowired
	private WorkShopDataHandler workShopDataHandler;
	
	@Override
	public ResultBean save(Plant plant) throws MESException{
		ResultBean resultBean = new ResultBean(true,"工厂创建成功!");
		if (!StringUtil.isNotBlank(plant.getInnerData().getName())
				&& !StringUtil.isNotBlank(plant.getInnerData().getPlantCode())) {
			resultBean.setSuccess(false);
			resultBean.setMessage("请检查工厂名称、工厂编码是否填写!");
			return resultBean;
		}
		
		if (!StringVerifyUtil.isstring(plant.getInnerData().getPlantCode())) {
		    resultBean.setSuccess(false);
		    resultBean.setMessage("工厂编码不是由英文、字母、下划线组成!!");
		    return resultBean;
		   }
		Plant plantByCode = plantDataHandler.getByCode(plant.getInnerData().getPlantCode());
		Plant plantByName = plantDataHandler.getByName(plant.getInnerData().getName());
		if (plantByCode.getInnerData() != null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("工厂编码不唯一!");
			return resultBean;
		}
		if (plantByName.getInnerData() != null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("工厂名称不唯一!");
			return resultBean;
		}
		ResultBean resultBean2 = plantDataHandler.save(plant);
		if(!resultBean2.isSuccess()) {
			resultBean.setSuccess(false);
			resultBean.setMessage(resultBean2.getMessage());
			return resultBean;
		}
		resultBean.setObject(resultBean2.getObject());
		return resultBean;
	}
	@Override
	public ResultBean update(Plant plant) throws MESException{
		ResultBean resultBean = new ResultBean(true,"工厂修改成功!");
		Plant plantById = plantDataHandler.getById(plant.getInnerData().getId());
		
		if (plantById.getInnerData() == null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("修改的工厂不存在!");
			return resultBean;
		}
		if (!StringVerifyUtil.isstring(plant.getInnerData().getPlantCode())) {
		    resultBean.setSuccess(false);
		    resultBean.setMessage("工厂编码不是由英文、字母、下划线组成!!");
		    return resultBean;
		   }
		if (!plant.getInnerData().getPlantCode().equals(plant.getInnerData().getPlantCode())
				&& !plant.getInnerData().getName().equals(plant.getInnerData().getName())) {
			resultBean.setSuccess(false);
			resultBean.setMessage("工厂编码、工厂名称不可修改!");
			return resultBean;
		}
			
		plantDataHandler.update(plant);
		resultBean.setObject(plant);
		return resultBean;
	}
	
	@Override
	public ResultBean delete(Plant plant) throws MESException{
		ResultBean resultBean = new ResultBean(true,"工厂删除成功!");
		List<Workshop> workShopList = workShopDataHandler.getWorkshopByPlantId(plant.getInnerData().getId());
		if (workShopList != null && workShopList.size() > 0) {
			resultBean.setSuccess(false);
			resultBean.setMessage("该工厂已被车间引用，无法删除!");
			return resultBean;
		}
		plantDataHandler.delete(plant);
		return resultBean;
	}
	@Override
	public Plant getById(long id) {
		return plantDataHandler.getById(id);
	}
	@Override
	public Plant getByName(String name) {
		return plantDataHandler.getByName(name);
	}
	@Override
	public Plant getByCode(String plantCode) {
		return plantDataHandler.getByCode(plantCode);
	}
	@Override
	public List<Plant> getAllPlant() {
		return plantDataHandler.getAllPlant();
	}
	@Override
	public List<Map<String, String>> queryPlantCodeIdNameList() {
		
		return plantDataHandler.queryPlantCodeIdNameList();
	}
	@Override
	public ResultBean getPlantByName(String name) {
		ResultBean resultBean = new ResultBean(true,"");
		List<Plant> listPlant = plantDataHandler.getPlantByName(name);
		resultBean.setObject(listPlant);
		return resultBean;
	}
}
